<?php

/**
 * Pluginfilterspec form.
 *
 * @package    ##PROJECT_NAME##
 * @subpackage form
 * @author     ##AUTHOR_NAME##
 * @version    SVN: $Id: sfDoctrineFormPluginTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
abstract class PluginfilterspecForm extends BasefilterspecForm
{
	public $searchType;

	public function setLabels()
	{
	  	$objDef = new objectDef('filterspec');
	  	foreach($objDef->attributes as $fieldName => $att){
	  		if($att['type']=='BO') $fieldName .= '_id'; 
	  		$this->offsetGet($fieldName)->getWidget()->setLabel($att['label']);
	  	}
	}
	
	/*
  * Set up du form : name format = aff (advanced filter form) in order to easy identify forms fields.
  * 
  * */
 public function setSearchType($type){
 	if($type=='quick'){
		$updateFunction = 'reRenderSearchBar();';
		$this->searchType = 'quick';
		$this->widgetSchema->setNameFormat('qff[%s]');
		$combostyle = 'searchSmallCombobox';
 	}else{
		$updateFunction = 'showAdvancedSearch();';
		$this->searchType = 'advanced';
		$combostyle = 'searchCombobox';
		$this->widgetSchema->setNameFormat('aff[%s]');
 	}
 	
 	
	$this->offsetGet('fieldName')->getWidget()->setAttributes(array(
			'class' => $combostyle,
	 		'onchange'=> $updateFunction,
		 ));
	$this->offsetGet('operator')->getWidget()->setAttributes(array(
			'class' => $combostyle,
	 		'onchange'=> $updateFunction,
		 )); 	
	$this->offsetGet('connector')->getWidget()->setdefault('and'); 	
 	
 	
 }
 /*
  * Sets the filter type from field type and operator.
  * 
  *  @param string $fieldName 		the field that's going to be used as a filter. If empty, the descriptor is set.
  *  @param string $table 			classname of the view's main object. 
  *  @param string $operator 		operator of the filter. Possible values : equal=>Equal; different=>Different; minor=>Minor; major=>Major; 
  *  																		  minorequal=>Minor or equal; majorequal=>Major or equal; 
  *  																		  between=> between; in=>in; is empty=>is empty; is not empty=>is not empty; 
  *  																		  contiens=>contiens; starts=>starts width; ends=>ends width;
  * 
  *  $retuns string 				the fieldtype of the filter.
  * */
 public function setFilterType($fieldName, $table, $operator = 'equal'){
		$objDef = new objectDef($table);
		if($fieldName==''){
			$fieldName = $objDef->descriptor;
		}
		$att = $objDef->attributes[$fieldName];	
		$type = $att['type'];
		$attributes = array();
		//Which fields to hide.
 		if($this->searchType=='quick'){
			$this->hideWidget('operator');
			$this->hideWidget('connector');
			$showUntil = true;
			$multiple = false;
		}else{
			if($operator == 'between'){
				$showUntil = true;
			}else{
				$showUntil = false;
			}
			if($operator =='in'){
				$multiple = true;
			}else{
				$multiple = false;
			}
			if(!$showUntil){
				$this->hideWidget('valueUntil');
			}
			
		}	
		if($type=='BO'){
			//$fieldName = $fieldName.'_id';
			$relatedObjDef = $objDef->getRelatedObjectdef($fieldName,0, false);
			$filter = '';
			if($multiple){
				$addEmpty = false;
			}else{
				$addEmpty = true;
			}
			$isnumeric = false;
			//$funcs = new erdfFuncs;
			$relatedTable = Doctrine::getTable($relatedObjDef->className);
			$id = $relatedTable->getIdentifier();
			$descriptor = $relatedObjDef->descriptor;
			$util = new Util;
			$options = $util->getOptions($relatedObjDef->className, $id, $descriptor, $filter, $addEmpty, $isnumeric);
			$this->widgetSchema['valueFrom'] = new sfWidgetFormChoice(
											array('multiple' => $multiple,
							  					  'expanded' => false,
							  					  'choices'  => $options,));	
			if($showUntil){	
				$this->widgetSchema['valueUntil'] = new sfWidgetFormChoice(
											array('multiple' => false,
							  					  'expanded' => false,
							  					  'choices'  => $options,));
			}											
			$attributes['class'] = 'searchCombobox';
			if($multiple){
				$attributes['class'] = 'searchChoiceList';	
			}		
		}
		elseif($type=='string'){
			 $this->widgetSchema['valueFrom'] = new sfWidgetFormInput();
			 if($showUntil) $this->widgetSchema['valueUntil'] = new sfWidgetFormInput();
			 $attributes=array('class' => 'searchText');
		}
		elseif($type=='integer'){
			 $this->widgetSchema['valueFrom'] = new sfWidgetFormInput();
			 if($showUntil) $this->widgetSchema['valueUntil'] = new sfWidgetFormInput();
			 $attributes=array('class' => 'searchText');
			 $attributes['tmt:pattern'] = 'integer';
			 $attributes['tmt:message'] = 'Vous devez saisir une valeur numerique';  			 
		}
		elseif($type=='float'){
			 $this->widgetSchema['valueFrom'] = new sfWidgetFormInput();
			 if($showUntil) $this->widgetSchema['valueUntil'] = new sfWidgetFormInput();
			 $attributes=array('class' => 'searchText');
			 $attributes['tmt:pattern'] = 'number';
			 $attributes['tmt:message'] = 'Vous devez saisir une valeur numerique';  			 
		}
		elseif($type=='date' or $type=='timestamp'){
			 $dateOptions = array();
			 if($type=='timestamp'){
				$dateOptions['istimestamp'] = true;	
			 }
			 $widgetDateName = sfConfig::get('app_form_datewidgetclass', 'sfWidgetFormDateJS');//'sfWidgetFormDateJQueryUI'; //
			 if($widgetDateName=='sfWidgetFormDateJQueryUI'){
			 	$dateOptions = array('theme'=>'/css/app/ui-lightness/jquery-ui.css');	
			 } 
		     $this->widgetSchema['valueFrom'] = new $widgetDateName($dateOptions);
			 if($showUntil) $this->widgetSchema['valueUntil'] = new $widgetDateName($dateOptions);
			 $attributes['class'] = 'searchDate';
			 $attributes['tmt:datepattern'] = 'DD/MM/YYYY';
			 $attributes['tmt:message'] = 'Vous devez saisir une date (JJ/MM/YYYY)';  
		}elseif($type=='boolean'){
			 $nbrChams = 1;
			 $attributes['class'] = 'searchRadioCheck';
			 $this->widgetSchema['valueFrom'] = new sfWidgetFormInputCheckbox();
			 if($showUntil) $this->widgetSchema['valueUntil'] = new sfWidgetFormInputHidden();
		}elseif($type=='enum'){
			$attributes['width'] = "300px";
			$util = new Util;
			$options = $util->getArrayFromStr($util->stripAccents2($att['enumValues']),';',true,'=>');
			
			$this->widgetSchema['valueFrom'] = new sfWidgetFormChoice(
											array('multiple' => false,
							  					  'expanded' => false,
							  					  'choices'  => $options,));	
			if($showUntil){	
				$this->widgetSchema['valueUntil'] = new sfWidgetFormChoice(
											array('multiple' => false,
							  					  'expanded' => false,
							  					  'choices'  => $options,));
			}											
			$attributes['class'] = 'searchCombobox';
			
		}
		if($type=='BO'){
			$attributes['width'] = "300px";
		} 
		if($multiple == true and $type!='BO'){
			$helps = $this->widgetSchema->getHelps();
			$helps['valueFrom'] = 'Comma separated values';
			if($showUntil) $helps['valueUntil'] = 'Comma separated values';
			$this->widgetSchema->setHelps($helps);
		}
		
		$this->offsetGet('valueFrom')->getWidget()->setAttributes($attributes);
		if($type=='BO'){
//			$attributes['onMouseOver'] = "enlarge('valueUntil');";
//			$attributes['onMouseOut'] = "ensmall('valueUntil');";
		}
		if($showUntil) $this->offsetGet('valueUntil')->getWidget()->setAttributes($attributes);
 		if($operator == 'is empty' or $operator == 'is not empty'){
			$this->hideWidget('valueFrom');
		}		

		$this->setDefault('fieldName',$fieldName);
		$this->setDefault('operator',$operator);
		$this->setDefault('className',$table);
		return $type;
		
  }
	
  /*
   * Sets the values of the fieldName widget following the object definition
   * 
   * @param string @table 		The classname of the object that's going to be filtered.
   * 
   * return void
   * */
  
  public function setFields($table, $quickSearch = false){
  		$i18N = sfContext::getInstance()->getI18N();
  	
  		$default = null;
		$fields = array();
		if($this->searchType=='quick'){
			$updateFunction = 'reRenderSearchBar()';
			$combostyle = 'searchSmallCombobox';
		}else{
			$updateFunction = 'showAdvancedSearch();';
			$combostyle = 'searchCombobox';
		}
		 
		$objDef = new objectDef($table);
		if($objDef){
			foreach($objDef->attributes as $fieldName => $att){
	      		if($att['search'] and $att['type']!='BOlist' and $att['type']!='List'){
	      			if(!isset($firstField)) $firstField = $fieldName;
	      			$label = $i18N->__($att['label'],null,'forms');
	      			if($label=='') $label = $fieldName;
	      			$fields[$fieldName] = $label;
	      			if($fieldName == $objDef->descriptor){
	      				$default = $fieldName;
	      			}
	      		}
			}
		}else{
			$objTable = Doctrine::getTable($table);
			//$objDef = $schema[$table];
			$columns = $objTable->getFieldNames();			
			foreach($columns as $fieldName){
				if(!isset($firstField)) $firstField = $fieldName;
				$fields[$fieldName] = $fieldName;
			}
		}
		
		$this->widgetSchema['fieldName'] =new sfWidgetFormChoice(array('multiple' => false,
							  'expanded' => false,
							  'choices'  => $fields));  
		$this->offsetGet('fieldName')->getWidget()->setAttributes(array(
			'class' => $combostyle,
	 		'onchange'=> $updateFunction,
		 ));
		 $objDef = new objectDef('filterpec');
		 if($objDef->classExists)
		 	$this->offsetGet('fieldName')->getWidget()->setLabel($objDef->attributes['fieldName']->label);
		 if(!is_null($default))
			$this->setDefault('field', $default);
		$this->setDefault('className',$table);
	 //$this->setFilterType($firstField,$table);
	 
	}

}
